package com.wsc.springbootrabbitmq.producer.callback;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

/**
 * 各种 callback
 */
@Component
public class MyCallbacks {

    /**
     * confirmCallback
     * @return
     */
    public RabbitTemplate.ConfirmCallback confirmCallback (){
        RabbitTemplate.ConfirmCallback confirmCallback = new RabbitTemplate.ConfirmCallback() {
            // p1:correlationData p2:ack p3:message
            @Override
            public void confirm(CorrelationData correlationData, boolean b, String s) {
                System.err.println("CorrelationData :"+correlationData);
                System.err.println("ack :"+b);
                if(!b){
                    System.err.println("消息异常处理....");
                }
            }
        };
        return confirmCallback;
    }

    /**
     * returnCallback
     * @return
     */
    public RabbitTemplate.ReturnCallback returnCallback (){
        RabbitTemplate.ReturnCallback returnCallback = new RabbitTemplate.ReturnCallback() {
            // p1:message p2:replyCode p3:replyText p4:exchange p5:routingKey
            @Override
            public void returnedMessage(Message message, int i, String s, String s1, String s2) {
                System.err.println("exchange :" +s1 + ",routingKey :"+s2 + "," +
                        "replyCode :"+i + ",replyText :"+s);
            }
        };
        return returnCallback;
    }

}
